package com.vichat.common;

import com.alibaba.fastjson.JSON;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 82116 on 2018/1/16.
 */
public class apiHttpPost {
    private static Log logger = LogFactory.getLog(apiHttpPost.class);
   private static String defaultUrl = "http://localhost:8090/rollApi/";
   //private static String defaultUrl = "http://47.100.112.4:6100/rollApi/";
    public static void main(String[] args){
    	
    	//mainShow();//监区首页数据展示
    	//getTask();//判断是否有未完成的任务
    	//addTask();//新增任务
    	//delTask();//取消任务
    	//showConvict();//出工前/收工前罪犯信息扫描展示
    	//addTaskConvict();//手动增加任务罪犯
    	//delTaskConvict();//手动删除任务罪犯
    	//getWorkplaces();//获取当前民警下所有车间
    	//rollCall();//点名/未点
    	//getTaskConvicts();//获取罪犯列表
    	//getConvictListByDistrict();//获取当前车间罪犯详情
    	//getRollTaskHistory();//获取历史记录
    }
    
    public static void getTask(){
        String url = defaultUrl + "getTask";
        RollTaskVO paramsVO = new RollTaskVO();
        paramsVO.setUid(300000001l);
        paramsVO.setTaskType(2);
        String json = JSON.toJSONString(paramsVO);
        logger.info("输入参数：" + json);
        String responseBody = post(url,json);
        logger.info("输出结果：" + responseBody);
        System.out.println(responseBody);
    }
    
    public static void mainShow(){
        String url = defaultUrl + "mainShow";
        RollTaskVO paramsVO = new RollTaskVO();
        paramsVO.setUid(300000001l);
        String json = JSON.toJSONString(paramsVO);
        logger.info("输入参数：" + json);
        String responseBody = post(url,json);
        logger.info("输出结果：" + responseBody);
        System.out.println(responseBody);
    }
    
    public static void addTask(){
        String url = defaultUrl + "addTask";
        RollTaskVO paramsVO = new RollTaskVO();
        paramsVO.setUid(300000001l);
        paramsVO.setDistrictId(32l);
        paramsVO.setTaskType(3);
        paramsVO.setConvictUids("300000101");
        paramsVO.setOutState(2);
        paramsVO.setOutType(1);
        paramsVO.setBackTime("2018-04-29 12:00:00");
        //paramsVO.setTaskId(14);
        //paramsVO.setNodeState(99);
        List<ConvictVO> voList = new ArrayList<ConvictVO>();
        ConvictVO vo = new ConvictVO();
        vo.setConvictUid(300000411l);
        vo.setAddType("6");
        vo.setRollType("2");
        voList.add(vo);
        
        ConvictVO vo2 = new ConvictVO();
        vo2.setConvictUid(300000102l);
        vo2.setRollType("3");
        vo2.setAddType("6");
        voList.add(vo2);
        
        paramsVO.setConvictVOList(voList);
        String json = JSON.toJSONString(paramsVO);
        logger.info("输入参数：" + json);
        String responseBody = post(url,json);
        logger.info("输出结果：" + responseBody);
        System.out.println(responseBody);
    }

    public static void showConvict(){
        String url = defaultUrl + "showConvict";
        RollTaskVO paramsVO = new RollTaskVO();
        paramsVO.setUid(300000001L);
        paramsVO.setNodeState(3);
        paramsVO.setTaskId(3l);
        String json = JSON.toJSONString(paramsVO);
        logger.info("输入参数：" + json);
        String responseBody = post(url,json);
        logger.info("输出结果：" + responseBody);
        System.out.println(responseBody);
    }
    
    public static void delTask(){
        String url = defaultUrl + "delTask";
        RollTaskVO paramsVO = new RollTaskVO();
        paramsVO.setUid(300000001L);
        paramsVO.setTaskId(8l);
        String json = JSON.toJSONString(paramsVO);
        logger.info("输入参数：" + json);
        String responseBody = post(url,json);
        logger.info("输出结果：" + responseBody);
        System.out.println(responseBody);
    }

    public static void addTaskConvict() {
    	String url = defaultUrl + "addTaskConvict";
        RollTaskVO paramsVO = new RollTaskVO();
        paramsVO.setUid(300000001L);
        paramsVO.setTaskId(8l);
        paramsVO.setAddType(4);
        String json = JSON.toJSONString(paramsVO);
        logger.info("输入参数：" + json);
        String responseBody = post(url,json);
        logger.info("输出结果：" + responseBody);
        System.out.println(responseBody);
    }
    
    public static void delTaskConvict() {
    	String url = defaultUrl + "delTaskConvict";
        RollTaskVO paramsVO = new RollTaskVO();
        paramsVO.setConvictUid(300000001L);
        paramsVO.setTaskId(8l);
        String json = JSON.toJSONString(paramsVO);
        logger.info("输入参数：" + json);
        String responseBody = post(url,json);
        logger.info("输出结果：" + responseBody);
        System.out.println(responseBody);
    }
    
    public static void getWorkplaces() {
    	String url = defaultUrl + "getWorkplaces";
        RollTaskVO paramsVO = new RollTaskVO();
        paramsVO.setUid(300000001L);
        String json = JSON.toJSONString(paramsVO);
        logger.info("输入参数：" + json);
        String responseBody = post(url,json);
        logger.info("输出结果：" + responseBody);
        System.out.println(responseBody);
    }
    
    public static void rollCall() {
    	String url = defaultUrl + "rollCall";
        RollTaskVO paramsVO = new RollTaskVO();
        paramsVO.setUid(300000001L);
        paramsVO.setTaskId(8l);
        String json = JSON.toJSONString(paramsVO);
        logger.info("输入参数：" + json);
        String responseBody = post(url,json);
        logger.info("输出结果：" + responseBody);
        System.out.println(responseBody);
    }
    
    public static void getTaskConvicts() {
    	String url = defaultUrl + "getTaskConvicts";
        RollTaskVO paramsVO = new RollTaskVO();
        paramsVO.setUid(300000001L);
        paramsVO.setTaskId(8l);
        String json = JSON.toJSONString(paramsVO);
        logger.info("输入参数：" + json);
        String responseBody = post(url,json);
        logger.info("输出结果：" + responseBody);
        System.out.println(responseBody);
    }
    
    public static void getConvictListByDistrict() {
    	String url = defaultUrl + "getConvictListByDistrict";
        RollTaskVO paramsVO = new RollTaskVO();
        paramsVO.setUid(300000001L);
        paramsVO.setTaskId(8l);
        String json = JSON.toJSONString(paramsVO);
        logger.info("输入参数：" + json);
        String responseBody = post(url,json);
        logger.info("输出结果：" + responseBody);
        System.out.println(responseBody);
    }
    
    public static void getRollTaskHistory() {
    	String url = defaultUrl + "getRollTaskHistory";
        RollTaskVO paramsVO = new RollTaskVO();
        paramsVO.setUid(300000001L);
        paramsVO.setTaskId(8l);
        String json = JSON.toJSONString(paramsVO);
        logger.info("输入参数：" + json);
        String responseBody = post(url,json);
        logger.info("输出结果：" + responseBody);
        System.out.println(responseBody);
    }
    
    /**
     * post 方法
     * @param url
     * @param json
     * @return
     */
    private static String post(String url, String json) {
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost method = new HttpPost(url);
        String responseBody = null;
        try {
            // 建立一个NameValuePair数组，用于存储欲传送的参数
            method.addHeader("Content-type", "application/json; charset=utf-8");
            method.setHeader("Accept", "application/json");
            method.setEntity(new StringEntity(json, Charset.forName("UTF-8")));
            long startTime = System.currentTimeMillis();
            HttpResponse response = httpClient.execute(method);
            long endTime = System.currentTimeMillis();
            int statusCode = response.getStatusLine().getStatusCode();
            logger.info("statusCode:" + statusCode);
            logger.info("调用API 花费时间(单位：毫秒)：" + (endTime - startTime));
            if (statusCode != HttpStatus.SC_OK) {
                logger.error("Method failed:" + response.getStatusLine());
            }
            // Read the response body
            responseBody = EntityUtils.toString(response.getEntity());
            logger.info("输出参数：" + responseBody);
        } catch (IOException e) {
            // 网络错误
            e.printStackTrace();
        } finally {
            return responseBody;
        }
    }
}
